#include <stdio.h>
#include <string.h>
#define MAX_CONTACTS 100

struct Contact
{
	char name[50];
	char phone[20];
	char email[50];
	char address[100];
};

struct Contact contacts[MAX_CONTACTS];
int count = 0;

void meau();
void add();
void Shanchu();
void Shuchu();
void search();
void Baocun();
void Jiazai();

int main()
{

	int choice;
	do
	{
		meau();
		scanf("%d", &choice);
		getchar();

		switch(choice)
		{
			case 1:
				add();
				break;
			case 2:
				Shanchu();
				break;
			case 3:
				Shuchu();
				break;
			case 4:
				search();
				break;
			case 5:
				Baocun();
				break;
			case 6:
				Jiazai();
				break;
			case 0:
				printf("退出系统...\n");
				break;
			default:
				printf("无效选择!\n");
		}
	}
	while(choice != 0);

	return 0;
}

void meau()
{
	printf("\n=== 通讯录系统 ===\n");
	printf("1. 添加联系人\n");
	printf("2. 删除联系人\n");
	printf("3. 显示所有联系人\n");
	printf("4. 查找联系人\n");
	printf("5. 保存通讯录\n");
	printf("6. 加载通讯录\n");
	printf("0. 退出\n");
	printf("请选择: ");
}

void add()
{
	if(count >= MAX_CONTACTS)
	{
		printf("通讯录已满\n");
		return;
	}

	printf("\n---添加联系人---\n");

	printf("姓名:");
	fgets(contacts[count].name, 50, stdin);
	contacts[count].name[strcspn(contacts[count].name, "\n")] = 0;//删除换行符

	printf("电话:");
	fgets(contacts[count].phone, 20, stdin);
	contacts[count].phone[strcspn(contacts[count].phone, "\n")] = 0;

	printf("邮箱:");
	fgets(contacts[count].email, 50, stdin);
	contacts[count].email[strcspn(contacts[count].email, "\n")] = 0;

	printf("地址:");
	fgets(contacts[count].address, 100, stdin);
	contacts[count].address[strcspn(contacts[count].address, "\n")] = 0;

	count++;
	printf("添加成功\n");

}

void Shanchu()
{
	if(count == 0)
	{
		printf("通讯录为空!\n");
		return;
	}

	char name[50];
	printf("\n---删除联系人---\n");
	printf("输入姓名:");
	fgets(name,50,stdin);
	name[strcspn(name, "\n")] = 0;

	int found = 0;
	for(int i = 0; i < count; i++)
	{
		if(strcmp(contacts[i].name,name) == 0)
		{
			if(i != count - 1)
			{
				strcpy(contacts[i].name, contacts[count-1].name);
				strcpy(contacts[i].phone, contacts[count-1].phone);
				strcpy(contacts[i].email, contacts[count-1].email);
				strcpy(contacts[i].address, contacts[count-1].address);
			}
			count--;
			found = 1;
			printf("删除成功!\n");
			break;
		}
	}
	if(found == 0)
	{
		printf("未找到该联系人!\n");
	}
}


void Shuchu()
{
	if(count == 0)
	{
		printf("通讯录为空!\n");
		return;
	}

	printf("\n--- 所有联系人 (%d) ---\n", count);
	for(int i = 0; i < count; i++)
	{
		printf("\n联系人 #%d\n", i+1);
		printf("姓名: %s\n", contacts[i].name);
		printf("电话: %s\n", contacts[i].phone);
		printf("邮箱: %s\n", contacts[i].email);
		printf("地址: %s\n", contacts[i].address);
	}
}

void search()
{
	if(count == 0)
	{
		printf("通讯录为空\n");
		return;
	}

	char name[50];
	printf("\n---查找联系人---\n");
	printf("请输入姓名:");
	fgets(name, 50, stdin);
	name[strcspn(name, "\n")] = 0;

	int found = 0;

	for(int i = 0; i < count; i++)
	{

		if(strcmp(contacts[i].name, name ) == 0)
		{
			printf("\n找到联系人:\n");
			printf("姓名: %s\n", contacts[i].name);
			printf("电话: %s\n", contacts[i].phone);
			printf("邮箱: %s\n", contacts[i].email);
			printf("地址: %s\n", contacts[i].address);
			found = 1;
			break;
		}
	}

	if(found == 0)
	{
		printf("未找到该联系人!\n");
	}
}

void Baocun()
{
	FILE *file = fopen("contacts.txt", "w");
	if(file == NULL)
	{
		printf("无法保存文件!\n");
		return;
	}

	for(int i = 0; i < count; i++)
	{
		fprintf(file, "%s|%s|%s|%s\n",
		        contacts[i].name,
		        contacts[i].phone,
		        contacts[i].email,
		        contacts[i].address);
	}

	fclose(file);
	printf("保存成功! (共%d个联系人)\n", count);
}

void Jiazai()
{
	FILE *file = fopen("contacts.txt", "r");
	if (file == NULL)
	{
		printf("无法加载文件，文件不存在或无法打开。\n");
		return;
	}

	count = 0; // 清空当前数据

	char line[256];
	while (fgets(line, sizeof(line), file) && count < MAX_CONTACTS)
	{

		char *name = strtok(line, "|");
		char *phone = strtok(NULL, "|");
		char *email = strtok(NULL, "|");
		char *address = strtok(NULL, "\n"); 

		if (!name || !phone || !email || !address)
		{
			continue; 
		}

		name[strcspn(name, "\n")] = 0;
		phone[strcspn(phone, "\n")] = 0;
		email[strcspn(email, "\n")] = 0;
		address[strcspn(address, "\n")] = 0;

		
		snprintf(contacts[count].name, 50, "%s", name);
		snprintf(contacts[count].phone, 20, "%s", phone);
		snprintf(contacts[count].email, 50, "%s", email);
		snprintf(contacts[count].address, 100, "%s", address);
		count++;
	}

	fclose(file);
	printf("成功加载%d个联系人。\n", count);
}




